package com.up.stores.controller;


import com.up.stores.entity.Address;
import com.up.stores.entity.Order1;
import com.up.stores.service.IOrderService;
import com.up.stores.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "订单模块")
@RestController
@RequestMapping("Order")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    //创建订单二合一
    @PostMapping("/create_Order")
    @ApiOperation(value = "普通用户||管理员创建订单")
    public JsonResult<Void> create(HttpSession session, Integer pro_id, Integer num) {
        String Type=getTypeFromSession(session);
        String Name = getUsernameFromSession(session);
        Integer Id = getuidFromSession(session);
        if(Type.equals("Normal")){
            orderService.createNorOrder(Id, pro_id, num, Name);
        }
       else if(Type.equals("Admin")){
            orderService.createAdmOrder(Id, pro_id, num, Name);
        }
        return new JsonResult<Void>(OK);
    }

    //购买状态的改变,应该是在拦截器里面写的，怎么搞
    @PutMapping("/change_status")
    @ApiOperation(value = "管理员改变订单状态")
    public JsonResult<Void> ChangeStatus(HttpSession session,
                                         Integer OrdId,
                                         Integer Status){
        if(!getTypeFromSession(session).equals("Admin")){
            //这里的报错怎么搞
            return new JsonResult<Void>(6004);
        }

        orderService.changeOrderStatus(OrdId,Status);
        return new JsonResult<Void>(OK);
    }

    //审核订单,也要在拦截器里面添加
    @PutMapping("/approve")
    @ApiOperation(value = "管理员审核订单改变状态")
    public JsonResult<Void> ApproveOrder(HttpSession session,
                                         Integer ord_id,
                                         Address address,
                                         Integer status){
        String name=getUsernameFromSession(session);
        Integer adm_id=getuidFromSession(session);
        orderService.approvalOrder(ord_id,address,status,adm_id);
        return new JsonResult<Void>(OK);
    }

    //查询指定状态的订单
    @GetMapping("/SearchStatudeOrder")
    @ApiOperation(value = "查询指定订单的状态")
    public JsonResult<List<Order1>> SearcherStatusOrder(HttpSession session,
                                                        Integer status){
        List<Order1> list=new ArrayList<>();
        String Type=getTypeFromSession(session);
        Integer id=getuidFromSession(session);
        if(Type.equals("Admin")){
            list=orderService.findOrderByStatus1AndAdmId(id,status);
        }
        else if(Type.equals("Normal")){
            list=orderService.findOrderByStatus1AndNorId(id,status);
        }
        return new JsonResult<List<Order1>>(OK,list);
    }

    //查询所有订单
    @GetMapping("/SearchOrder")
    @ApiOperation(value = "查询当前用户的所有订单")
    public JsonResult<List<Order1>> SearchOrder(HttpSession session){
        String Type=getTypeFromSession(session);
        Integer uid=getuidFromSession(session);
        List<Order1> list=new ArrayList<>();
        if(Type.equals("Admin")){
            list=orderService.findOrderByAdmId(uid);
        }
        else if(Type.equals("User")){
            list=orderService.findOrderByNorId(uid);
        }
        return new JsonResult<List<Order1>>(OK,list);
    }

    //这里也需要注册一个拦截器,只允许商家通过
    @GetMapping("/DeliverProduct")
    @ApiOperation(value = "商家发货接口")
    public JsonResult<Void> DeliverProduct(HttpSession session,
                                           String longId,
                                           Integer ordId){
        String name=getUsernameFromSession(session);
        orderService.DeliverPro(ordId,longId,name);
        return new JsonResult<Void>(OK);
    }

    //这里也需要注册一个拦截器,只允许管理员通过
    @GetMapping("/RecieverPro")
    @ApiOperation(value = "管理员收货接口")
    public JsonResult<Void> Receipt(HttpSession session,
                                    Integer ordId){
        orderService.RecieverPro(ordId);
        return new JsonResult<Void>(OK);
    }

}